﻿using System;
using System.Text;
using WeatherSystem.Application.ContentAdapter;
using WeatherSystem.Application.ContentAdapter.Base;
using WeatherSystem.Application.ContentAdapter.EventAdapter;
using WeatherSystem.Net;

namespace WeatherSystem
{
    class Program
    {
        private static HttpServer Server;
        static void Main(string[] args)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            UdpServer udpServer = new UdpServer(2222);
            udpServer.Start();
            Program.Log("UDP服务启动...");
            ContentManager.Init();
            EventManager.AddAdapter(new QrCodeAdapter());
            EventManager.AddAdapter(new SubscribeAdapter());
            EventManager.AddAdapter(new ClickAdapter());
            Program.Log("初始化内容管理器...");
            DataBase.Initialise();
            Program.Log("初始化数据库...");
            Server = new HttpServer(80);
            Application.Application.LoadApp(typeof(Application.Index.Index));
            Server.HttpGotRequest += Server_HttpGotRequest;
            Server.Start();
            Program.Log("服务启动成功.");
            while (true)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                var cmd = Console.ReadLine();
                if(cmd == "stop")
                {
                    Program.Warning("服务已经停止.");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Environment.Exit(Environment.ExitCode);
                }
                if(cmd == "sign")
                {
                    Program.Log("验证模式已启动");
                    Application.Index.Index.Signature = true;
                }
                if(cmd == "normal")
                {
                    Program.Log("验证模式已关闭");
                    Application.Index.Index.Signature = false;
                }
                else
                {
                    Program.Error("未知命令.");
                }
            }
        }

        public static void Log(string msg)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(DateTime.Now.ToString("[yyyy-MM-dd hh:mm:ss]"));
            Console.WriteLine(msg);
        }

        public static void Log(string format, params object[] args)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(DateTime.Now.ToString("[yyyy-MM-dd hh:mm:ss]"));
            Console.WriteLine(format, args);
        }

        public static void Warning(string msg)
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write(DateTime.Now.ToString("[yyyy-MM-dd hh:mm:ss]"));
            Console.WriteLine(msg);
        }

        public static void Warning(string format, params object[] args)
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write(DateTime.Now.ToString("[yyyy-MM-dd hh:mm:ss]"));
            Console.WriteLine(format, args);
        }

        public static void Error(string msg)
        {
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Write(DateTime.Now.ToString("[yyyy-MM-dd hh:mm:ss]"));
            Console.WriteLine(msg);
        }

        public static void Error(string format, params object[] args)
        {
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Write(DateTime.Now.ToString("[yyyy-MM-dd hh:mm:ss]"));
            Console.WriteLine(format, args);
        }

        private static HttpResponseArgs Server_HttpGotRequest(HttpArgs args)
        {
            return Application.Application.Handle(args);
        }
    }
}
